import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPoolClass {
    //任务队列
    private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<Runnable>(1000);
    public MyThreadPoolClass(int n) {   //n表示创建集合线程
        //先创建出n个线程
        for(int i=0;i<n;i++) {
            Thread t=new Thread(()->{
                while(true) {
                    try {
                        //循环从队列中获取任务
                        Runnable runnable=queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }
    }
    //往任务队列里添加任务
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
public class MyThreadPool {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolClass myThreadPoolClass=new MyThreadPoolClass(4);
        for(int i=0;i<100;i++) {
            int id=i;
            myThreadPoolClass.submit(()->{
                System.out.println("执行任务"+id+","+Thread.currentThread().getName());
            });
        }
    }
}
